package com.netintech.project.system.controller;

import java.util.List;
import java.util.Set;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.netintech.common.constant.Constants;
import com.netintech.common.utils.ServletUtils;
import com.netintech.core.security.LoginUser;
import com.netintech.core.security.service.SysLoginService;
import com.netintech.core.security.service.SysPermissionService;
import com.netintech.core.security.service.TokenService;
import com.netintech.core.web.domain.RequestResult;
import com.netintech.project.system.domain.SysMenu;
import com.netintech.project.system.domain.SysUser;
import com.netintech.project.system.service.SysMenuService;

/**
 * 登录验证
 *
 * @author admin
 */
@RestController
public class SysLoginController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     * @param username 用户名
     * @param password 密码
     * @param code     验证码
     * @param uuid     唯一标识
     * @return 结果
     */
    @PostMapping("login")
    public RequestResult login(@RequestBody SysUser sysUser) {
        String username = sysUser.getUserName();
        String password = sysUser.getPassword();
        // 生成请求头中的token
        LoginUser loginUser  = loginService.login(username, password, null,
                null);
        SysUser user = loginUser.getUser();
        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        return RequestResult.success(ImmutableMap.builder()
                .put(Constants.TOKEN, loginUser.getToken())
                .put("user",user)
//                .put("roles",roles)
                .put("authority",permissionService.getMenuPermissionWithoutF(user))
                .put("menu",permissionService.getMenuPermission2(user))
                .build());
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @PostMapping("getInfo")
    public RequestResult getInfo() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        return RequestResult.success(ImmutableMap.builder().put("user",user)
                .put("roles",roles)
                .put("permissions",permissions)
                .build());
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public RequestResult getRouters() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return RequestResult.success(menuService.buildMenus(menus));
    }
}
